<template>
    <el-container style="height: 98vh; border: 1px solid #eee;background: #fff">
        <SideBar />
        <el-container>
            <el-header style="width: 95%;margin-top: 10px;">
                <HeadBar />
            </el-header>
            <el-main id="main">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" class="demo-ruleForm">
                    <el-form-item label="手机电话" prop="phoneNumber">
                        <el-input v-model="ruleForm.phoneNumber"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱" prop="email">
                        <el-input v-model="ruleForm.email"></el-input>
                    </el-form-item>
                    <el-form-item label="紧急联系人" prop="emergencyContact">
                        <el-input v-model="ruleForm.emergencyContact"></el-input>
                    </el-form-item>
                    <el-form-item label="紧急联系人电话" prop="emergencyContactPhone">
                        <el-input v-model="ruleForm.emergencyContactPhone"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                        <el-button @click="resetForm">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
import SideBar from '@/components/SideBar.vue'
import HeadBar from '@/components/HeadBar.vue'
import request from '@/utils/request';

export default {
    components: { SideBar, HeadBar },
    data() {
        return {
            userInfo: null,
            ruleForm: {
                studentId: sessionStorage.getItem("studentId"),
                phoneNumber: '',
                email: '',
                emergencyContact: '',
                emergencyContactPhone: '',
            },
            rules: {
                phoneNumber: [
                    { required: true, message: '请输入手机号码', trigger: 'blur' },
                    { pattern: /^[1][3-9][0-9]{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                    { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                ],
                emergencyContact: [
                    { required: true, message: '请输入紧急联系人姓名', trigger: 'blur' }
                ],
                emergencyContactPhone: [
                    { required: true, message: '请输入紧急联系人电话', trigger: 'blur' },
                    { pattern: /^[1][3-9][0-9]{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
                ]
            },
        };
    },
    created() {
        const studentId = sessionStorage.getItem("studentId");
        request.get('/student/findByID?studentId=' + studentId).then(res => {
            if (res.data.state == '0') {
                this.userInfo = res.data.data;
                this.ruleForm = {
                    studentId: this.userInfo.studentId,
                    phoneNumber: this.userInfo.phoneNumber,
                    email: this.userInfo.email,
                    emergencyContact: this.userInfo.emergencyContact,
                    emergencyContactPhone: this.userInfo.emergencyContactPhone,
                }
                console.log('userInfo', this.userInfo);
            } else {
                this.$message({
                    type: "error",
                    message: "获取用户信息失败"
                })
            }
        })
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    var url = "/student/changeInfo?" + new URLSearchParams(this.ruleForm).toString();
                    request.get(url).then(res => {
                        if (res.data.state == '0') {
                            this.$message({
                                message: '修改成功',
                                type: 'success'
                            });
                            this.$router.push("/SelfInfoView");
                        } else {
                            this.$message({
                                message: '修改失败',
                                type: 'error'
                            });
                        }
                    });
                } else {
                    this.$message({
                        message: '表单验证失败',
                        type: 'error'
                    });
                    return false;
                }
            });
        },
        resetForm() {
            this.ruleForm.phoneNumber = this.userInfo.phoneNumber
            this.ruleForm.email = this.userInfo.email
            this.ruleForm.emergencyContact = this.userInfo.emergencyContact
            this.ruleForm.emergencyContactPhone = this.userInfo.emergencyContactPhone
        }
    }
}
</script>

<style scoped>
#main {
    width: 95%;
    margin-top: 30px;
    align-items: center;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    margin-left: 50px
}
</style>